package chapter15;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 10:37
 */
public class CounterThread extends Thread {
    private Counter counter;

    public CounterThread(Counter counter) {
        this.counter = counter;
    }

    public static void main(String[] args) {
        int num = 1000;
        Counter counter = new Counter();
        Thread[] threads = new Thread[num];
        for (int i = 0; i < num; i++) {
            Thread thread = new CounterThread(counter);
            threads[i] = thread;
            thread.start();
        }

        for (int i = 0; i < num; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(counter.getCount());
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            counter.incr();
        }
    }
}
